/**
 * 
 */
package aiproject3.models;

import java.util.HashMap;

/**
 * @author Chris
 *
 */
public class Trigram implements KnowledgeBase<Character, Integer> {

	private HashMap<String, Integer> _data;
	
	private String _lastChars;
	
	public Trigram() {
		_data = new HashMap<String, Integer>();
	}
	
	public void addToModel(Character unit) {
		// First check if there is a last chars... otherwise just instantiate last chars
		if (_lastChars != null) {
			if (!_data.containsKey(_lastChars + String.valueOf(unit))) {
				_data.put(_lastChars + String.valueOf(unit), 1);
			}
			else {
				_data.put(_lastChars + String.valueOf(unit), _data.get(_lastChars + String.valueOf(unit)) + 1);
			}
		} else
			_lastChars = String.valueOf(unit) + String.valueOf(unit);
		_lastChars = String.valueOf(_lastChars.charAt(1)) + String.valueOf(unit);
	}

	public boolean contains(Character unit) {
		if (_lastChars == null)
			return false;
		else
			return _data.containsKey(_lastChars + String.valueOf(unit));
	}	

	public Integer getValueFromModel(Character unit) {
		return _data.get(_lastChars + String.valueOf(unit));
	}

	public int getNumUnits() {
		return _data.keySet().size();
	}
	
}
